#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# runtime
############################################################################
#    Copyright (C) 2007 by Håvard Gulldahl                                 #
#    havard@gulldahl.no                                                    #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import sys, types, logging, os
from lib.openanything import openAnything

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)-8s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    stream=sys.stdout)

logging.debug('importerer BeautifulSoup')
import BeautifulSoup
logging.debug('importerer storm')
import storm.locals as storm
logging.debug('importerer jeje')
import jeje, jeje.fingerprinter, jeje.collector

DEBUG=False
_store = None

def prepare_database():
    db_filename = os.path.abspath('ejejeweb/ejeje.db')
    if DEBUG:
        db_filename = '' # in memory
    connection_string = 'sqlite:' + db_filename #+ '?debug=true'
    logging.debug('Kobler til storm: %s', db_filename)
    database = storm.create_database(connection_string)
    store = storm.Store(database)
    #Note that Store objects are not threadsafe. You should create one Store per thread in your application, passing them the same backend Database object
    jeje._store = store # FIXME: make a proper connection manager

    return store

class dummy_postjournal(object):
    organisasjon_id = 12

def slurp(noe):
    suppe = BeautifulSoup.BeautifulSoup(openAnything(noe).read(),
                                        smartQuotesTo=None,
                                        convertEntities='html')
    return suppe

if __name__ == '__main__':
    #s = jeje._store
    s = prepare_database()
    if '-d' in sys.argv[1:]:
        sys.argv.remove('-d')
        DEBUG = True
        logging.getLogger().setLevel(logging.DEBUG)
    for z in sys.argv[1:]:
        logging.info('leser inn suppa: %s', z)
        suppe = slurp(z)
        logging.debug('laster parseren')
        parser = jeje.fingerprinter.identifiser_postjournal(suppe)(slurp)
        logging.debug('fant parser %s', parser)
        parser.enkeltjournal = jeje.ejeje_enkeltjournal()
        #parser.enkeltjournal.uri = u'http://test/'
        parser.enkeltjournal.uri = u'http://postliste.tromso.kommune.no/postjournal?date=15.06.2010'
        dummy = s.get(jeje.ejeje_postjournal, 3)
        parser.enkeltjournal.postjournal = dummy
        poster = parser.parse_enkeltjournal(suppe, parser.enkeltjournal.uri)
        
        logging.info('fant %i poster', len(poster))
